import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from "@/views/Home";
import AdminHome from "@/views/page/admin/AdminHome";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Home',
        component: Home,
        redirect: '/homepage',
        children: [
            {
                path: 'homepage',
                name: 'HomePage',
                component: () => import('@/views/page/HomePage'),
                meta: {
                    title: '首页'
                }
            },
            {
                path: 'details/:id',
                name: 'Details',
                component: () => import('@/views/page/Details'),
                meta: {
                    title: '商品信息'
                }
            },
            {
                path: 'chat',
                name: 'Chat',
                component: () => import('@/views/page/Chat'),
                meta: {
                    title: '交流模块'
                }
            },
            {
                path: 'userinfo',
                name: 'UserInfo',
                component: () => import('@/views/page/UserInfo'),
                meta: {
                    title: '个人中心'
                }
            },
            {
                path: 'cart',
                name: 'Cart',
                component: () => import('@/views/page/Cart'),
                meta: {
                    title: '购物车'
                }
            },
            {
                path: 'adminlogin',
                name: 'AdminLogin',
                component: () => import('@/views/page/AdminLogin'),
                meta: {
                    title: '管理员登录'
                }
            }
        ]
    },
    {
        path: '/adminhome',
        name: 'AdminHome',
        component: AdminHome,
        redirect: '/adminhome/product',
        children: [
            {
                path: 'product',
                name: 'Product',
                component: () => import('@/views/page/admin/Product'),
                meta: {
                    title: '商品管理'
                }
            },
            {
                path: 'users',
                name: 'Users',
                component: () => import('@/views/page/admin/Users'),
                meta: {
                    title: '用户管理'
                }
            },
            {
                path: 'order',
                name: 'Order',
                component: () => import('@/views/page/admin/Order'),
                meta: {
                    title: '订单管理'
                }
            },
            {
                path: 'leader',
                name: 'Leader',
                component: () => import('@/views/page/admin/Leader'),
                meta: {
                    title: '团长管理'
                }
            },
            {
                path: 'log',
                name: 'Log',
                component: () => import('@/views/page/admin/Log'),
                meta: {
                    title: '系统日志'
                }
            }
        ]
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/Login'),
        meta: {
            title: '登录'
        }
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import('@/views/Register'),
        meta: {
            title: '注册'
        }
    }
]

//重写push和replace方法
let originPush=VueRouter.prototype.push;
let originReplace=VueRouter.prototype.replace;
VueRouter.prototype.push=function (location,resolve,reject){
    if(resolve&&reject){
        originPush.call(this,location,resolve,reject);
    }else {
        originPush.call(this,location,()=>{},()=>{})
    }
}
VueRouter.prototype.replace=function (location,resolve,reject){
    if (resolve&&reject){
        originReplace.call(this,resolve,reject)
    }else{
        originReplace.call(this,location,()=>{},()=>{})
    }
}

const router = new VueRouter({
    routes
})

export default router
